//
//  LotteryDetailsViewController.m
//  PlayerWorld
//
//  Created by 1 on 16/5/5.
//  Copyright © 2016年 刘成. All rights reserved.
//
#define UISCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width
#define UISCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#import "LotteryDetailsViewController.h"
#import "LotteryDetailsTableViewCell.h"

#import "UIKit+AFNetworking.h"
#import "LotteryDataModel.h"
@interface LotteryDetailsViewController ()

@end

@implementation LotteryDetailsViewController
{
    NSMutableArray *_listArr;
}
-(void)obtainData
{
    // 启动系统风火轮
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    //服务器给的域名
    NSString *domainStr = @"http://666.lefun6.com/";
    NSLog(@"案件基督教的基督教的积极地：%@",self.idStr);
    //创建一个可变字典
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    //往字典里面添加需要提交的参数
    [parametersDic setObject:@"Game" forKey:@"Model"];
    [parametersDic setObject:@"GetHistoryWinCode" forKey:@"Action"];
    [parametersDic setObject:self.idStr forKey:@"Id"];
    [parametersDic setObject:@"10" forKey:@"Num"];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    //以post的形式提交，POST的参数就是上面的域名，parameters的参数是一个字典类型，将上面的字典作为它的参数
    [manager POST:domainStr parameters:parametersDic progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 解析成功隐藏系统风火轮
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
        NSArray *array = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        _listArr = [NSMutableArray array];
        for (NSDictionary *dic in array) {
            LotteryDataModel *model = [[LotteryDataModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_listArr addObject:model];
        }
        [LotteryDetailstableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        // 解析失败隐藏系统风火轮(可以打印error.userInfo查看错误信息)
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self obtainData];
    self.tabBarController.tabBar.hidden = YES;  
    self.view.backgroundColor = [UIColor orangeColor];
    self.title = self.name;
    
    LotteryDetailstableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT-64) style:UITableViewStylePlain];
    LotteryDetailstableView.dataSource = self;
    LotteryDetailstableView.delegate = self;
    [LotteryDetailstableView registerNib:[UINib nibWithNibName:@"LotteryDetailsTableViewCell" bundle:nil]  forCellReuseIdentifier:@"cell"];
    LotteryDetailstableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:LotteryDetailstableView];

}

- (CGFloat)tableView:(UITableView *)atableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 90;
}

//设置表视图中的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _listArr.count;
    
}

//设置单元格中的数据
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdent = @"LotteryDetailsTableViewCell";
    LotteryDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdent];
    if (cell == nil) {
        cell = [[LotteryDetailsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdent];
    }
    LotteryDataModel *model = [_listArr objectAtIndex:indexPath.row];
    cell.lotteryDetailsLabel.text = model.W_Issue;
    cell.lotteryDetailsLabel2.text = model.WinTime;
    NSLog(@"%@",model.W_Code);
    NSArray *arr = [model.W_Code componentsSeparatedByString:@","];
    if ([self.idStr isEqualToString:@"14"]) {
        NSInteger add = 0;
        NSMutableArray *aarray = [NSMutableArray array];
        for (NSInteger i = 0; i < arr.count; i++) {
            if (i % 4 == 0) {
                if (i != 0) {
                    NSString *addStr = [NSString stringWithFormat:@"%ld",add];
                    [aarray addObject:[addStr substringFromIndex:addStr.length - 1]];
                }
                add = [arr[i] integerValue];
                continue;
            }
            add += [arr[i] integerValue];
            if (i == arr.count - 1) {
                NSString *addStr = [NSString stringWithFormat:@"%ld",add];
                [aarray addObject:[addStr substringFromIndex:addStr.length - 1]];
            }
        }
        arr = [NSArray arrayWithArray:aarray];
    }
    [cell.lotteryNumber1Button setTitle:[arr objectAtIndex:0] forState:UIControlStateNormal];
    [cell.lotteryNumber2Button setTitle:[arr objectAtIndex:1] forState:UIControlStateNormal];
    [cell.lotteryNumber3Button setTitle:[arr objectAtIndex:2] forState:UIControlStateNormal];
    if (arr.count>3) {
        [cell.lotteryNumber4Button setTitle:[arr objectAtIndex:3] forState:UIControlStateNormal];
        [cell.lotteryNumber5Button setTitle:[arr objectAtIndex:4] forState:UIControlStateNormal];
    }
    else{
        cell.lotteryNumber4Button.hidden = YES;
        cell.lotteryNumber5Button.hidden = YES;
    }
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}
//单元格点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//    LotteryDetailsViewController *lDVC= [[LotteryDetailsViewController alloc] init];
//    [self.navigationController pushViewController:lDVC animated:YES];
    
}

@end
